package com.cd.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cd.bean.Contact;
import com.cd.bean.Dictionary;
import com.cd.bean.News;
import com.cd.bean.Science;
import com.cd.bean.Technology;
import com.cd.service.contact.ContactService;
import com.cd.service.dictionary.DictionaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 数据字典(科研平台)
 *@Author zhk
 *@Date 201712-22
 **/
@RequestMapping("/dic")
@Controller
public class DictionaryController {

	@Autowired
	DictionaryService dictionaryService;
	@Autowired
	ContactService contactService;
	
	/**
	 * 去编辑界面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goEdit")
	public ModelAndView goEdit(@RequestParam(value="id")Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Dictionary dic = dictionaryService.findById(id);
		mv.addObject("dic", dic);
		mv.setViewName("admin/kypt/kypt_edit");
		return mv;
	}
	
	/**
	 *跳转到添加新闻页面
	 * @return
	 */
	@RequestMapping("/goAdd")
	public ModelAndView goAdd() throws Exception {
		ModelAndView mv=new ModelAndView();
		mv.setViewName("admin/kypt/kypt_add");
		return mv;
	}
	/**
	 * 编辑（后台）
	 * @param id
	 * @param name
	 * @param content
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/edit")
	public ModelAndView eidt(@RequestParam(value="DICTIONARIES_ID")Integer id,@RequestParam(value="NAME")String name,
			                 @RequestParam(value="CONTENT")String content ) throws Exception {
			ModelAndView mv = new ModelAndView();
			Dictionary dic = dictionaryService.findById(id);
			dic.setNAME(name);
			dic.setCONTENT(content);
			dic.setPUBLISH_DATE(new Date());
			dictionaryService.edit(dic);
			mv.setViewName("redirect:list.do");
		return mv;
	}
	
	/**
	 * 新增
	 * @param name
	 * @param content
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public ModelAndView save(@RequestParam(value="NAME") String name,@RequestParam(value="CONTENT") String content) throws Exception {
		ModelAndView mv = new ModelAndView();
		//int total = dictionaryService.size();
		Dictionary dic = new Dictionary();
		//dic.setDICTIONARIES_ID(total+1);
		dic.setNAME(name);
		dic.setCONTENT(content);
		dic.setPUBLISH_DATE(new Date());
		dictionaryService.save(dic);
		mv.setViewName("redirect:list.do");
		return mv;
	}
	/**
	 * 列表（后台,带分页）
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 10);//开始分页,每页10条记录
	    List<Dictionary> list = dictionaryService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("admin/kypt/kypt_list");
		return mv;
	}
	

	/**
	 * 查询（带分页）
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listDic")
	public ModelAndView findAllDictionary(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 15);//开始分页
	    List<Dictionary> list = dictionaryService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("info/kypt/kypt");
		return mv;
	}
	
	/**
	 * 通过id查询
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById")
	public ModelAndView findById(@RequestParam(value = "id") Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Dictionary dic = null;
		Dictionary nextdic = null;
		Dictionary predic = null;
		int total = dictionaryService.size();//获取总记录
		int nextId = id+1;
		int preId = id-1;
		if(id <= total){
			if(id >= 1){
				dic = dictionaryService.findById(id);//当前条
				predic = dictionaryService.findById(preId);//前一条
				nextdic = dictionaryService.findById(nextId);//下一条	
			}else {
				dic = dictionaryService.findById(id+1);
			}
		}else{
			dic = dictionaryService.findById(id-1);
		}
		mv.addObject("dic", dic);
		mv.addObject("predic", predic);
		mv.addObject("nextdic", nextdic);
		mv.setViewName("info/kypt/kypt_show");
		return mv;
	}
	
	/**
	 * 去查看内容页面(后台)
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goContent")
	public ModelAndView goContent(@RequestParam(value="id")Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Dictionary dic = dictionaryService.findById(id);
		mv.addObject("dic", dic);
		mv.setViewName("admin/kypt/kypt_content");
		return mv;
	}
	
	
	/**
	 * 删除
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public ModelAndView delete(@RequestParam(value="id")Integer id) throws Exception{
		ModelAndView mv = new ModelAndView();
		dictionaryService.delete(id);
		mv.setViewName("redirect:list.do");
		return mv;
	}
	
	
	/**
	 * 跳转关于我们
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goAboutUs")
	public ModelAndView goAboutUs() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("info/gywm/gywm");
		return mv;
	}
	/**
	 * 跳转部门职责
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goDepartment")
	public ModelAndView goDepartment() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("info/gywm/bmzz");
		return mv;
	}
	/**
	 * 跳转专家风采
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goExpert")
	public ModelAndView goExpert(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 16);//开始分页,每页16个
	    List<Contact> list = contactService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("info/gywm/zjfc");
		return mv;
	}
	/**
	 * 
	 * 批量删除
	 * @param request
	 * @param response
	 */
	@RequestMapping("/del")  
	public void batchDeletes(HttpServletRequest request, HttpServletResponse response) {  
	    String items = request.getParameter("delitems");
	    System.out.println(items);  
	    String[] strs = items.split(",");  
	  
	    for (int i = 0; i < strs.length; i++) {  
	        try {  
	            int a = Integer.parseInt(strs[i]);  
	            dictionaryService.delete(a);  
	        } catch (Exception e) {  
	        }  
	    }  
	} 
}
